//
// Created by hps on 24-4-19.
//

#include "AddNoteBookDialog.h"

namespace hps {
AddNoteBookDialog::AddNoteBookDialog(AddNoteBookData *resData)
    : m_resData(resData) {
    m_layout = new QVBoxLayout();
    m_bookNameInputer = new hps::LineInputer(std::string("名称"));
    m_bookPathInputer = new hps::LineInputer(std::string("路径"));
    m_bookPathButton = new QPushButton();
    m_informationInputer = new hps::LineInputer(std::string("描述"));
    m_opcButtons = new hps::ChooseOrCancelButton();
    init();
    setUi();
    rspFunc();
}

AddNoteBookDialog::AddNoteBookDialog() {
    m_layout = new QVBoxLayout();
    m_bookNameInputer = new hps::LineInputer(std::string("名称"));
    m_bookPathInputer = new hps::LineInputer(std::string("路径"));
    m_bookPathButton = new QPushButton();
    m_informationInputer = new hps::LineInputer(std::string("描述"));
    m_opcButtons = new hps::ChooseOrCancelButton();
    init();
    setUi();
    rspFunc();
}
void AddNoteBookDialog::init() {
    this ->setWindowTitle("新建笔记本");
    m_layout->setContentsMargins(50, 60, 50, 20);
    m_layout->setSpacing(30);
    m_bookPathButton->setIcon(QIcon("/home/hps/hps/Markdown-cpp/src/data/icon/tabWidgetMore.svg"));
    m_bookPathButton->setIconSize(CONFIG()->getTabWidgetIconSize());
    m_bookPathButton->setObjectName("tabWidgetTabBarButton");
}
void AddNoteBookDialog::setUi() {
    this->setLayout(m_layout);
    m_bookPathInputer->getLayout()->insertWidget(2, m_bookPathButton);
    m_layout->insertWidget(0, m_bookNameInputer);
    m_layout->insertWidget(1, m_bookPathInputer);
    m_layout->insertWidget(2, m_informationInputer);
    m_layout->insertWidget(3, m_opcButtons);
}
void AddNoteBookDialog::rspFunc() {
    QObject::connect(m_opcButtons->getOKButton(), &QPushButton::clicked, this, &AddNoteBookDialog::clickedOk);
    QObject::connect(m_opcButtons->getCanselButton(), &QPushButton::clicked, this, &AddNoteBookDialog::clickedCansel);
    QObject::connect(m_bookPathButton, &QPushButton::clicked, this, &AddNoteBookDialog::clickedPathButton);
}
void AddNoteBookDialog::clickedOk() {
    if (m_resData == nullptr) {
        QMessageBox::critical(this, "Error", "resData is null!");
        std::cerr << "resData is null" << __FILE__ << ":" << __LINE__ << std::endl;
    }
    m_resData->_name = m_bookNameInputer->getLineEditComment();
    m_resData->_path = m_bookPathInputer->getLineEditComment();
    m_resData->_information = m_informationInputer->getLineEditComment();
    if (m_resData->_name.empty() || m_resData->_path.empty()) {
        QMessageBox::critical(this, "Error", "请输入笔记本名以及路径!");
        return ;
    }
    accept();
}
void AddNoteBookDialog::clickedCansel() {
    reject();
}
void AddNoteBookDialog::clickedPathButton() {
    QString directoryPath = QFileDialog::getExistingDirectory(nullptr, "选择路径", QDir::homePath());
    if (! directoryPath.isEmpty()) {
        m_bookPathInputer ->setText(directoryPath);
    } else {
        return;
    }
}
} // hps